#include "stdafx.h"
#include "Lighting.h"

Lighting::Lighting() : cObject()
{
	//...
}

void Lighting::Init(char *szFileNFG, char *fileTGA)
{
	cObject::Init(GL_TEXTURE_2D, &fileTGA, 1, szFileNFG);


	//Initshader
	m_Shader = LightingShader();
	m_Shader.Init("../Resources/Shaders/LightingShaderVS.vs", "../Resources/Shaders/LightingShaderFS.fs");
}

void Lighting::Draw(ESContext *esContext)
{
	glUseProgram(m_Shader.program);
	glBindBuffer(GL_ARRAY_BUFFER, m_Model->GetVertexID());
	if(m_Shader.VertPosition != -1)
	{
		glEnableVertexAttribArray(m_Shader.VertPosition);
		glVertexAttribPointer(m_Shader.VertPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	}
	if(m_Shader.VertTexCood != -1)
	{
		glEnableVertexAttribArray(m_Shader.VertTexCood);
		glVertexAttribPointer(m_Shader.VertTexCood, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3));
	}
	if(m_Shader.VertNorm != -1)
	{
		glEnableVertexAttribArray(m_Shader.VertNorm);
		glVertexAttribPointer(m_Shader.VertNorm, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char*)0 + sizeof(Vector3) + sizeof(Vector2));
	}

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, m_Texture->GetTextureID()[0]);
	if(m_Shader.Texture != -1)
	{
		glUniform1i(m_Shader.Texture, 0);
	}

	if(m_Shader.WVPMatrix != -1)
	{
		Matrix maViewProjection = Globals::MainCamera->GetVPMatrix();
		glUniformMatrix4fv(m_Shader.WVPMatrix, 1, GL_FALSE, (GLfloat*)&(m_maLocalW * maViewProjection).m[0][0]);
	}
	if(m_Shader.WMatrix != -1)
	{
		glUniformMatrix4fv(m_Shader.WMatrix, 1, GL_FALSE, (GLfloat*)&m_maLocalW.m[0][0]);
	}

	if(m_Shader.CameraPosition != -1)
	{
		Vector3 cameraPos = Globals::MainCamera->GetPosition();
		glUniform3f(m_Shader.CameraPosition, cameraPos.x, cameraPos.y, cameraPos.z);
	}

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Model->GetIndiceID());
	glDrawElements(GL_TRIANGLES, m_Model->GetIndiceNumber(), GL_UNSIGNED_INT, 0);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
}

Lighting::~Lighting()
{
}